package com.gopay.common.security.validcode.manager.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.gopay.common.constants.validcode.CodeType;
import com.gopay.common.domain.vs.VsValidCodeDtl;
import com.gopay.common.security.validcode.dao.ValidCodeDAO;
import com.gopay.common.util.DateUtils;

/**
 * 个人绑定验证码实现
 * @PersonBindValidCodeManagerImpl.java
 * @author lizheng
 * @2013-11-8 上午9:35:49  www.gopay.com.cn Inc.All rights reserved.
 */
@Service("personBindValidCodeManager")
public class PersonBindValidCodeManagerImpl extends AbstractValidCode {

    @Resource(name = "validCodeDAO")
    private ValidCodeDAO validCodeDAO;

    @Override
    protected VsValidCodeDtl generateCode(CodeType type, String voID) {
        VsValidCodeDtl code = new VsValidCodeDtl();

        // 生成随机码
        String uid = UUID.randomUUID().toString();
        uid = uid.substring(0, 6).toUpperCase();

        // 当前时间
        Date curDate = DateUtils.getSystemDate();

        code.setCode(uid);
        code.setType(type.value);
        code.setVoID(voID);
        code.setExpir(expirTime);
        code.setCreated(curDate);
        code.setModified(curDate);

        return code;
    }

    @Override
    public boolean valid(CodeType type, String voID, String code) {
        boolean flag = false;

        List<VsValidCodeDtl> list = validCodeDAO.getValidCodes(type, voID);
        if (list == null || list.size() == 0) {
            return false;
        }

        // 当前时间
        Date currentDate = DateUtils.getSystemDate();
        long curDate = currentDate.getTime();

        VsValidCodeDtl validCode = null;
        for (Iterator<VsValidCodeDtl> it = list.iterator(); it.hasNext();) {
            validCode = it.next();

            long dbDate = validCode.getCreated().getTime();
            long curr = (curDate - dbDate);
            long expir = expirTime * 1000;

            // 验证码是否过期 AND 验证码是否匹配(忽略大小写)
            if (curr <= expir && StringUtils.equalsIgnoreCase(code, SensitiveInfoUtils.smartDecryptWithCheck(validCode.getCode()))) {
                flag = true;

                break;
            }
        }

        updateModified(list);

        return flag;
    }

    /**
     * 批量更新修改时间
     * 
     * @param list
     */
    private void updateModified(List<VsValidCodeDtl> list) {
        if (list == null || list.size() == 0) {
            return;
        }

        // 当前时间
        Date currentDate = DateUtils.getSystemDate();

        VsValidCodeDtl validCode = null;
        for (Iterator<VsValidCodeDtl> it = list.iterator(); it.hasNext();) {
            validCode = it.next();

            // 更新修改时间，以证明验证过
            validCode.setModified(currentDate);
            validCodeDAO.update(validCode);
        }
    }

	@Override
	public boolean valid(CodeType type, String voID, String code, String timeOut) {
		// TODO Auto-generated method stub
		return false;
	}
}
